package interview;

import java.util.Arrays;

public class interview_16_21 {
    public int[] findSwapValues(int[] array1, int[] array2) {
        int[] result=new int[2];
        int len1 = array1.length;
        int len2 = array2.length;
        Arrays.sort(array1);
        Arrays.sort(array2);
        long sum1=0;
        long sum2=0;
        for(int i=0;i<len1;++i){
            sum1+=array1[i];
        }
        for(int i=0;i<len2;++i){
            sum2+=array2[i];
        }
        if(sum1-sum2+sum2!=sum1){
            return new int[0];
        }
        int target= (int) (sum1-sum2);
        int index1=0;
        int index2=0;
        while(index1<len1&&index2<len2){
            if((array1[index1]-array2[index2])*2<target){
                index1++;
            }else if((array1[index1]-array2[index2])*2>target){
                index2++;
            }else{
                result[0]=array1[index1];
                result[1]=array2[index2];
                return result;
            }
        }
        return new int[0];
    }
}
